// pages/index/styletest.js
var api = require('../../utils/api.js');
var istap=0
Page({

  /**
   * 页面的初始数据
   */
  data: {
    citys: ['武汉','十堰', '荆门', '襄阳', '荆州', '宜昌', '孝感', '黄石', '咸宁', '恩施', '黄冈', '鄂州', '天门', '信阳', '洛阳'],
    steps:['view1','view2','view3','view4','view5'],
    toView:0,
    isscroll:false,
    selecteditems:[-1,-1,-1,-1,-1],
    index:null,
    inputmobile:''
  },

  bindCitysChange(e){
    this.setData({
      index: e.detail.value
    })
  },

  nextstep(e){
    //是否选择了选项
    if (this.data.selecteditems[this.data.toView] == -1) {
      wx.showToast({
        title: '至少选择一个选项',
        icon: 'none'
      })
      return false;
    }else if(this.data.index==null){
      wx.showToast({
        title: '请选择您的城市',
        icon: 'none'
      })
      return false;
    }

    if (istap == 0 && this.data.toView < this.data.selecteditems.length-1){
      istap=1;
      this.setData({
        isscroll: true
      })
      this.setData({
        toView: this.data.toView + 1
      })
    }
  },
  prestep(e) {
    console.log(istap)
    if (istap==0&&this.data.toView>0) {
        istap = 1;
        this.setData({
          isscroll: true
        })
        this.setData({
          toView: this.data.toView - 1
        })
    }
  },
  onscroll(e){
    console.log(e)
    console.log(e.detail.scrollLeft)
    console.log(Math.abs(e.detail.deltaX) * this.data.toView)
    if (Math.abs(e.detail.deltaX) * this.data.toView - e.detail.scrollLeft == 0 ){
        this.setData({
          isscroll: false
        })
        istap=0
      }
  },

  touchstart(e){
    if (this.data.isscroll){
      this.setData({
        isscroll: false
      })
    }
  },

  selectitem(e){
    let id=e.target.dataset.id;
    let step=e.target.dataset.step;
    
    if (this.data.selecteditems[step] == id){
      this.data.selecteditems[step] = -1;
    }else{
      this.data.selecteditems[step] = id;
    }
    this.setData({
      selecteditems: this.data.selecteditems
    })
  },

  formSubmit(e) {
    //提交表单数据
    if (this.data.selecteditems[this.data.toView] == -1) {
      wx.showToast({
        title: '至少选择一个选项',
        icon: 'none'
      })
      return false;
    }
    api.post({
      url: '/portal/diyform/insert',
      data: {
        city: this.data.citys[this.data.index],
        mobile:this.data.inputmobile,
        name:' ',
        source:'风格测试',
        styleimg: this.data.selecteditems[this.data.toView]
      },
      success: data => {
        if (data.code == "1") {
          wx.showToast({ title: '报名成功', icon: 'success' })
        }
        if (data.code == "0") {
          wx.showToast({ title: data.msg, icon: 'none' })
        }
      }
    })
  },

  inputmobile(e){
    this.setData({
      inputmobile:e.detail.value
    })
  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
  
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})